package com.xy.blog.user.service.impl;

import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.core.springmvc.result.page.PageResp;
import com.xy.blog.user.constants.SysDictTypes;
import com.xy.blog.user.controller.model.req.query.AttrDictQuery;
import com.xy.blog.user.dao.impl.AttrDictDaoImpl;
import com.xy.blog.user.dao.impl.SysDictDaoImpl;
import com.xy.blog.user.dao.model.AttrDictDO;
import com.xy.blog.user.dao.model.SysDictDO;
import com.xy.blog.user.exception.constants.AttrDictErrorCode;
import com.xy.blog.user.exception.constants.SysDictErrorCode;
import com.xy.blog.user.mapstruct.AttrDictConverter;
import com.xy.blog.user.service.IAttrDictService;
import com.xy.blog.user.service.model.AttrDictBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2019/11/17 4:24 下午
 */
@Service
public class AttrDictServiceImpl implements IAttrDictService {


    @Autowired
    private AttrDictConverter attrDictConverter;

    @Autowired
    private AttrDictDaoImpl attrDictDaoImpl;

    @Autowired
    private SysDictDaoImpl sysDictDaoImpl;


    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void saveAttrDict(AttrDictBO item) throws BaseException {
        AttrDictDO attrDictDO = attrDictConverter.bo2Do(item);

        SysDictDO sysDict = new SysDictDO();
        sysDict.setId(item.getTypeId());
        sysDict.setType(SysDictTypes.ATTR_DICT);
        List<SysDictDO> select = sysDictDaoImpl.select(sysDict);

        if (select == null || select.isEmpty()) {
            throw new BaseException(SysDictErrorCode.KEY_NOT_EXITS);
        }
        SysDictDO sysDictDO = select.get(0);
        attrDictDO.setValueType(sysDictDO.getValue());
        attrDictDO.setType(sysDictDO.getSKey());
        attrDictDaoImpl.save(attrDictDO);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateAttrDictById(AttrDictBO item) {
        AttrDictDO attrDictDO = attrDictConverter.bo2Do(item);
        AttrDictDO queryByName = new AttrDictDO();
        queryByName.setName(item.getName().trim());
        List<AttrDictDO> selectByName = attrDictDaoImpl.select(queryByName);
        if (selectByName != null && !selectByName.isEmpty()) {
            AttrDictDO result = selectByName.get(0);
            if (result.getId().equals(item.getId())) {
                throw new BaseException(AttrDictErrorCode.USER_ATTR_NAME_EXITS);
            }
        }

        AttrDictDO queryByCode = new AttrDictDO();
        queryByCode.setCode(item.getCode());
        List<AttrDictDO> select = attrDictDaoImpl.select(queryByCode);
        if (select != null && !select.isEmpty()) {
            AttrDictDO result = select.get(0);
            if (result.getId().equals(item.getId())) {
                throw new BaseException(AttrDictErrorCode.USER_ATTR_CODE_EXITS);
            }
        }

        Long typeId = item.getTypeId();
        SysDictDO byId = sysDictDaoImpl.getById(typeId);
        String sKey = byId.getSKey();
        String value = byId.getValue();
        attrDictDO.setType(sKey);
        attrDictDO.setValueType(value);
        attrDictDaoImpl.updateById(attrDictDO);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Long id) {
        attrDictDaoImpl.deleteById(id);
    }

    @Override
    public PageResp<AttrDictBO> listByPage(PageReq<AttrDictQuery> req) {
        PageResp<AttrDictDO> attrDictDOPageResp = attrDictDaoImpl.listByPage(req);
        PageResp<AttrDictBO> attrDictBOPageResp = attrDictConverter.do2Bo(attrDictDOPageResp);
        attrDictBOPageResp.getList().forEach(item->{
            String type = item.getType();
            SysDictDO sysDictDO = new SysDictDO();
            sysDictDO.setSKey(type);
            List<SysDictDO> select = sysDictDaoImpl.select(sysDictDO);
            if (select != null && !select.isEmpty()) {
                SysDictDO sysDictDORes = select.get(0);
                item.setTypeName(sysDictDORes.getName());
            }
        });
        return attrDictBOPageResp;
    }

    @Override
    public AttrDictBO getById(Long id) {
        AttrDictBO attrDictBO = attrDictConverter.do2Bo(attrDictDaoImpl.getById(id));
        SysDictDO sysDictDO = new SysDictDO();
        sysDictDO.setSKey(attrDictBO.getType());
        List<SysDictDO> select = sysDictDaoImpl.select(sysDictDO);
        if (select != null && !select.isEmpty()) {
            SysDictDO sysDictDORes = select.get(0);
            attrDictBO.setTypeId(sysDictDORes.getId());
        }
        return attrDictBO;
    }


}
